package com.jxc.jxc01.grc.web;

import com.alibaba.fastjson.JSON;
import com.jxc.jxc01.grc.entity.Procurement;
import com.jxc.jxc01.grc.entity.RespResult;
import com.jxc.jxc01.grc.service.IProcurementService;
import com.jxc.jxc01.grc.service.impl.ProcurementSreviceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/queryProcurement")
public class QueryProcurementServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        System.out.println("/queryProcurement");
        IProcurementService procurementService = new ProcurementSreviceImpl();
        List<Procurement> procurements = procurementService.queryProcurement();
        System.out.println(procurements);
        //接受session
        RespResult procurements1 = (RespResult) request.getSession().getAttribute("procurements");

        if (procurements1 == null){
            int count = procurementService.queryProcurementTotalCount();
            RespResult respResult = new RespResult();
            respResult.setCode(0);
            respResult.setMessage("ok");
            respResult.setCount(count);
            respResult.setData(procurements);
            System.out.println(respResult);
            writer.print(JSON.toJSONString(respResult));
        }else {
            writer.println(JSON.toJSONString(procurements1));
            //删除session
            request.getSession().invalidate();
        }
        writer.close();
    }
}
